﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：OrderManage.ascx
//
//文件功能描述：订单管理UI层
// 
//创建标识：2007.11.21 李鹏
//创建描述：
// 
//修改标识：2007.11.28李鹏
//修改描述：修改异常捕捉,分页操作
//
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;

public partial class UserControls_OrderManage : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            DocumentService documentService = new DocumentService();

            //
            //获取订单列表
            //
            string strStartTime = String.Empty;
            string strEndTime = String.Empty;
            int iPayStatus = 2;
            int iInvoiceStatus = 2;
            int iArchingStatus = 0;
            int iPageSize = 10;
            int iPageIndex = 1;

            //
            //绑定第一页的数据
            //
            DataSet ds = documentService.GetOrderListForPaging(iPageSize, iPageIndex, iPayStatus, iInvoiceStatus, iArchingStatus, strStartTime, strEndTime);
            GridviewBind(ds);

            //
            //隐藏控件保存共有多少条记录
            //
            int iOrders = documentService.GetOrderListRows(iPayStatus, iInvoiceStatus, iArchingStatus, strStartTime, strEndTime);
            hdfRecords.Value = iOrders.ToString();

            //
            //绑定分页按钮
            //
            BindToPaging(iPageSize, iPageIndex);

        }//if (!this.IsPostBack)
    }

    #region //绑定分页按钮
    void BindToPaging(int iPageSize,int iPageIndex)
    {
        DocumentService documentService = new DocumentService();
        //
        //读取订单数
        //
        int iOrders = Convert.ToInt32(hdfRecords.Value.Trim());

        if (iOrders > 0)
        {
            //
            //有订单
            //

            //
            //订单数
            //
            lblRecordCount.Text = "共 " + iOrders.ToString() + " 条";
            lblPageCount.Visible = true;
            lblNoRecord.Visible = false;

            //
            //计算总页数
            //
            int iPages = iOrders / iPageSize;
            if (iOrders % iPageSize != 0)
            {
                iPages += 1;
            }//if(iOrders % iPageSize != 0)
            lblPageCount.Text = "共 "+ iPages.ToString() + " 页";
            lblPageCount.Visible = true;

            //
            //有多页
            //
            if (iPages > 1)
            {
                btnFirst.Visible = true;
                btnPrev.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;
            }
            else
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }

            //
            //当前页
            //
            lblCurrentIndex.Text = "第 "+iPageIndex+" 页";
            lblCurrentIndex.Visible = true;
        }
        else
        {
            //
            //没有订单
            //
            lblNoRecord.Visible = true;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
            lblCurrentIndex.Visible = false;

            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

        }//if(iOrders > 0)
    }


    /// <summary>
    /// 按订单编号查询时,绑定分页控件
    /// </summary>
    void BindPagingByOrderID()
    {
        if (gdvOrder.Rows.Count > 0)
        {
            //
            //一条订单
            //
            lblNoRecord.Visible = false;
            lblPageCount.Text = "共 1 页";
            lblRecordCount.Text = "共 1 条";
            lblCurrentIndex.Text = "第 1 条";

            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }
        else
        {
            //
            //没有订单
            //
            lblNoRecord.Visible = true;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
            lblCurrentIndex.Visible = false;

            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }//if(gdvOrder.Rows.Count > 0)
    }//BindPagingByOrderID()

    #endregion

    /// <summary>
    /// 进入管理页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvOrder_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "manage")
        {
            int iIndex = Convert.ToInt32(e.CommandArgument);
            string strOrderID = this.gdvOrder.DataKeys[iIndex].Value.ToString();
            string strUrl = "~/SalesMan/OrderDetail.aspx?OrderInfoID=" + strOrderID;
            Response.Redirect(strUrl);
        }//if (e.CommandName == "manage")
    }


    /// <summary>
    /// 删除订单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void delete_Click(object sender, ImageClickEventArgs e)
    {
        //
        //获取订单号
        //
        string strOrderID = ((ImageButton)sender).CommandArgument.ToString();

        //
        //逻辑删除,将订单归档
        //
        int iSaleManID = Convert.ToInt32(Session["userID"]);
        DocumentService documentService = new DocumentService();

        documentService.UpdateArchivingStatus(strOrderID, iSaleManID, false);

        //
        //修改记录总条数
        //
        int iRecords = Convert.ToInt32(hdfRecords.Value.Trim()) - 1;
        hdfRecords.Value = iRecords.ToString();

        //
        //更新数据集
        //
        strOrderID = this.txtOrderID.Text.Trim();
        string strStartTime = this.txtStartTime.Text.Trim();
        string strEndTime = this.txtEndTime.Text.Trim();
        int iPayStatus = Convert.ToInt32(this.dropPayStatus.SelectedValue);
        int iInvoiceStatus = Convert.ToInt32(this.dropInvoiceStatus.SelectedValue);
        int iArchingStatus = Convert.ToInt32(this.dropArchingStatus.SelectedValue);
        int iPageSize = 10;

        //
        //获取当前页
        //
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(2);
        strPageIndex = strPageIndex.Substring(0, strPageIndex.Length - strPageIndex.LastIndexOf(" "));
        int iPageIndex = Convert.ToInt32(strPageIndex);

        this.SearchOrders(strOrderID, strStartTime, strEndTime, iPayStatus, iInvoiceStatus, iArchingStatus, iPageSize, iPageIndex);
    }


    /// <summary>
    /// 根据条件搜索订单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        //
        //设置输入数据格式是否正确标志
        //
        bool blnSuccess = false;

        //
        //配置查询条件
        //
        string strOrderID = this.txtOrderID.Text.Trim();
        string strStartTime = this.txtStartTime.Text.Trim();
        string strEndTime = this.txtEndTime.Text.Trim();
        int iPayStatus = Convert.ToInt32(this.dropPayStatus.SelectedValue);
        int iInvoiceStatus = Convert.ToInt32(this.dropInvoiceStatus.SelectedValue);
        int iArchingStatus = Convert.ToInt32(this.dropArchingStatus.SelectedValue);
       
        //
        //验证输入时间格式是否正确
        //
        try
        {
            if (strStartTime != String.Empty)
            {
                Convert.ToDateTime(strStartTime);
            }
            if (strEndTime != String.Empty)
            {
                Convert.ToDateTime(strEndTime);
            }
            blnSuccess = true;
        }
        catch (FormatException)
        {
            blnSuccess = false;
        }

        //
        //隐藏控件保存共有多少条记录
        //
        int iOrders = (new DocumentService()).GetOrderListRows(iPayStatus, iInvoiceStatus, iArchingStatus, strStartTime, strEndTime);
        hdfRecords.Value = iOrders.ToString();
        int iPageSize = 10;
        int iPageIndex = 1;

        //
        //查询满足条件的数据
        //
        if (blnSuccess)
        {
            this.SearchOrders(strOrderID, strStartTime, strEndTime, iPayStatus, iInvoiceStatus, iArchingStatus, iPageSize, iPageIndex);
        }
    }

   /// <summary>
    /// 根据条件搜索订单
   /// </summary>
   /// <param name="strOrderID">订单号</param>
   /// <param name="strStartTime">开始时间</param>
   /// <param name="strEndTime">结束时间</param>
   /// <param name="iPayStatus">收款状态</param>
   /// <param name="iInvoiceStatus">发货状态</param>
   /// <param name="iArchingStatus">归档状态</param>
   /// <param name="iPageSize">每页记录数</param>
   /// <param name="iPageIndex">当前页</param>
    protected void SearchOrders(string strOrderID, string strStartTime, string strEndTime, int iPayStatus, int iInvoiceStatus, int iArchingStatus, int iPageSize, int iPageIndex)
    {
        DataSet ds = new DataSet();

        DocumentService documentService = new DocumentService();
        if (strOrderID != String.Empty)
        {
            //
            //按订单号查询
            //
            ds = documentService.GetOrderList(strOrderID);
            GridviewBind(ds);

            //
            //绑定分页按钮
            //
            BindPagingByOrderID();
        }
        else
        {
            //
            //按其他条件查询
            //

            //绑定第一页的数据
            ds = documentService.GetOrderListForPaging(iPageSize, iPageIndex, iPayStatus, iInvoiceStatus, iArchingStatus, strStartTime, strEndTime);
            GridviewBind(ds);

            //
            //绑定分页按钮
            //
            BindToPaging(iPageSize, iPageIndex);

        }//if (strOrderID != String.Empty)
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="ds"></param>
    private void GridviewBind(DataSet ds)
    {
        this.gdvOrder.DataSource = ds.Tables[0].DefaultView;
        this.gdvOrder.DataBind();
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        //
        //获取当前页
        //
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(2);
        strPageIndex = strPageIndex.Substring(0, strPageIndex.Length - strPageIndex.LastIndexOf(" "));
        int iPageIndex = Convert.ToInt32(strPageIndex);

        //
        //获取总页数
        //
        string strPageCount = lblPageCount.Text.Trim();
        strPageCount = strPageCount.Substring(2);
        strPageCount = strPageCount.Substring(0, strPageCount.Length - strPageCount.LastIndexOf(" "));
        int iPageCount = Convert.ToInt32(strPageCount);

        //
        //判断操作类型
        //
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "prev"://上一页
                if (iPageIndex > 1)
                {
                    iPageIndex -= 1;
                }
                break;
            case "next"://下一页
                if (iPageIndex < iPageCount)
                {
                    iPageIndex += 1;
                }
                break;
            case "last"://最后一页
                iPageIndex = iPageCount;
                break;
            default://首页
                iPageIndex = 1;
                break;
        }//switch (arg)

        //
        //设置输入数据格式是否正确标志
        //
        bool blnSuccess = false;

        //
        //配置查询条件
        //
        string strOrderID = this.txtOrderID.Text.Trim();
        string strStartTime = this.txtStartTime.Text.Trim();
        string strEndTime = this.txtEndTime.Text.Trim();
        int iPayStatus = Convert.ToInt32(this.dropPayStatus.SelectedValue);
        int iInvoiceStatus = Convert.ToInt32(this.dropInvoiceStatus.SelectedValue);
        int iArchingStatus = Convert.ToInt32(this.dropArchingStatus.SelectedValue);
        
        //验证输入时间合法性
        try
        {
            if (strStartTime != String.Empty)
            {
                Convert.ToDateTime(strStartTime);
            }
            if (strEndTime != String.Empty)
            {
                Convert.ToDateTime(strEndTime);
            }
            blnSuccess = true;

        }
        catch (FormatException)
        {
            blnSuccess = false;
        }
        int iPageSize = 10;

        //
        //查询下一页记录
        //
        if (blnSuccess)
        {
            this.SearchOrders(strOrderID, strStartTime, strEndTime, iPayStatus, iInvoiceStatus, iArchingStatus, iPageSize, iPageIndex);
        }
    }
}
